using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SharpCore.Data;
using SharpCore.Utilities;

namespace Dating.DA
{
	public class PersonalityMappingDA
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors
		public PersonalityMappingDA()
		{
		}

		public PersonalityMappingDA(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the PersonalityMapping table.
		/// </summary>
		public  int Insert(PersonalityMapping personalityMapping)
		{
			ValidationUtility.ValidateArgument("personalityMapping", personalityMapping);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", personalityMapping.TenantID),
				new SqlParameter("@QuestionID", personalityMapping.QuestionID),
				new SqlParameter("@ValueID", personalityMapping.ValueID),
				new SqlParameter("@Order", personalityMapping.Order)
			};

			personalityMapping.MappingID = Convert.ToInt32(SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "PersonalityMappingInsert", parameters));
			return personalityMapping.MappingID;
		}

		/// <summary>
		/// Updates a record in the PersonalityMapping table.
		/// </summary>
		public  int Update(PersonalityMapping personalityMapping)
		{
			ValidationUtility.ValidateArgument("personalityMapping", personalityMapping);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@MappingID", personalityMapping.MappingID),
				new SqlParameter("@TenantID", personalityMapping.TenantID),
				new SqlParameter("@QuestionID", personalityMapping.QuestionID),
				new SqlParameter("@ValueID", personalityMapping.ValueID),
				new SqlParameter("@Order", personalityMapping.Order)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PersonalityMappingUpdate", parameters));
		}

		/// <summary>
		/// Deletes a record from the PersonalityMapping table by its primary key.
		/// </summary>
		public  int Delete(int mappingID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@MappingID", mappingID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PersonalityMappingDelete", parameters));
		}

		/// <summary>
		/// Deletes a record from the PersonalityMapping table by a foreign key.
		/// </summary>
		public  int DeleteAllByQuestionID(int questionID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@QuestionID", questionID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PersonalityMappingDeleteAllByQuestionID", parameters));
		}

		/// <summary>
		/// Deletes a record from the PersonalityMapping table by a foreign key.
		/// </summary>
		public  int DeleteAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PersonalityMappingDeleteAllByTenantID", parameters));
		}

		/// <summary>
		/// Selects a single record from the PersonalityMapping table.
		/// </summary>
		public  PersonalityMapping Select(int mappingID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@MappingID", mappingID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "PersonalityMappingSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MakePersonalityMapping(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the PersonalityMapping table.
		/// </summary>
		public  List<PersonalityMapping> SelectAll(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
			};
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "PersonalityMappingSelectAll", parameters))
			{
				List<PersonalityMapping> personalityMappingList = new List<PersonalityMapping>();
				while (dataReader.Read())
				{
					PersonalityMapping personalityMapping = MakePersonalityMapping(dataReader);
					personalityMappingList.Add(personalityMapping);
				}

				return personalityMappingList;
			}
		}

		/// <summary>
		/// Selects all records from the PersonalityMapping table by a foreign key.
		/// </summary>
		public  List<PersonalityMapping> SelectAllByQuestionID(int questionID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@QuestionID", questionID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "PersonalityMappingSelectAllByQuestionID", parameters))
			{
				List<PersonalityMapping> personalityMappingList = new List<PersonalityMapping>();
				while (dataReader.Read())
				{
					PersonalityMapping personalityMapping = MakePersonalityMapping(dataReader);
					personalityMappingList.Add(personalityMapping);
				}

				return personalityMappingList;
			}
		}

		/// <summary>
		/// Selects all records from the PersonalityMapping table by a foreign key.
		/// </summary>
		public  List<PersonalityMapping> SelectAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "PersonalityMappingSelectAllByTenantID", parameters))
			{
				List<PersonalityMapping> personalityMappingList = new List<PersonalityMapping>();
				while (dataReader.Read())
				{
					PersonalityMapping personalityMapping = MakePersonalityMapping(dataReader);
					personalityMappingList.Add(personalityMapping);
				}

				return personalityMappingList;
			}
		}

		/// <summary>
		/// Creates a new instance of the PersonalityMapping class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected  PersonalityMapping MakePersonalityMapping(SqlDataReader dataReader)
		{
			PersonalityMapping personalityMapping = new PersonalityMapping();
			personalityMapping.MappingID = SqlClientUtility.GetInt32(dataReader, "MappingID", 0);
			personalityMapping.TenantID = SqlClientUtility.GetInt32(dataReader, "TenantID", 0);
			personalityMapping.QuestionID = SqlClientUtility.GetInt32(dataReader, "QuestionID", 0);
			personalityMapping.ValueID = SqlClientUtility.GetInt32(dataReader, "ValueID", 0);
			personalityMapping.Order = SqlClientUtility.GetInt32(dataReader, "Order", 0);

			return personalityMapping;
		}

		#endregion
	}
}
